﻿/*
Класс работы с социальной сетью
*/

package game.network {
	import flash.events.ProgressEvent;
	import flash.utils.getDefinitionByName;
	import game.objects.GameObject;
	import game.objects.CompileObject;
	import game.errors.GameError;
	import game.ui.DesktopAuth;
	import game.network.api.*;
	import game.objects.UserApiObject;
	import game.events.*;
	import game.Config;
	import game.objects.UserObject;
	
	public class Network extends GameObject {
		
		public static const TEST:String		= "test";
		public static const VK:String		= "vk";
		public static const FACEBOOK:String	= "fb";
		
		private var params:Object;
		private var url:String;
		
		public var progress:Number = 0;
		
		public var api:INetwork;
		
		public function Network():void {
			
		}
		
		public function init( params:Object ):void {
			this.dispatchEvent( new NetworkEvent ( NetworkEvent.INIT ) );
			
			this.params	= params.parameters;
			this.url	= params.url;
			
			switch (config.compile.player) {
				case CompileObject.External:
					config.host = "";
					initTest();
				break;
				case CompileObject.ActiveX: case CompileObject.PlugIn:
					setAutoNetwork();
					switch (config.me.network) {
						case Network.VK:
							initVK();
						break;
						case Network.FACEBOOK:
							initFB();
						break;
						case Network.TEST:
							initTest();
						break;
						default:
							parseError();
						break;
					}
				break;
				case CompileObject.Desktop:
					config.host = "";
					openDesktop();
				break;
				default:
					parseError();
				break;
			}
			
		}
		
		private function setAutoNetwork():void {
			
		}
		
		private function openDesktop():void {
			//открывает окно с соц. сетями
			step = 1/3;
			
			var desktopAuth:DesktopAuth = new DesktopAuth;
			desktopAuth.addEventListener( NetworkEvent.OAUTH_ERROR, socialErrorHandler );
			desktopAuth.addEventListener( NetworkEvent.OAUTH_SUCCESS, socialSuccessHandler );
			stage.addChild(desktopAuth);
			
		}
		
		private function socialErrorHandler (event:NetworkEvent):void {
			//ошибка данных из соц сетей
			error (GameError.INIT_ERROR, 1);
		}
		
		private function socialSuccessHandler (event:NetworkEvent):void {
			//получены данные из соц сети
			try {
				event.target.parent.removeChild(event.target);
			} catch (e:Error) {
				trace ('error Network::socialSuccessHandler');
			}
			
			if (!api) {
				switch (config.me.network) {
					case Network.VK:
						api = new vkDesktop;
					break;
					case Network.FACEBOOK:
						api = new fbDesktop;
					break;
					default:
						parseError();
					break;
				}
				
				step = 2/3;
				
				api.getProfiles(config.me.uid, apiGetMyProfile);
				
			}
			
			this.dispatchEvent( new NetworkEvent ( NetworkEvent.GET_TOKEN ) );
			
		}
		
		private function apiGetMyProfile(users:Vector.<UserApiObject>):void {
			if (users.length==0) return;
			
			config.me.applyApiUser(users[0]);
			
			step = 3/3;
		}
		
		private function initVK():void {
			step = 1/3;
			
			api = new vkPlugin (params);
			api.addEventListener(VKLocalEvent.CONN_INIT, function (event:VKLocalEvent):void {
				step = 2/3;
				api.getProfiles(null, apiGetMyProfile);
			});
			
		}
		
		private function initFB():void {
			step = 1/3;
			
			api = new fbPlugin;
			api.addEventListener(FBLocalEvent.CONN_INIT, function (event:FBLocalEvent):void {
				step = 2/3;
				api.getProfiles(config.me.uid, apiGetMyProfile);
			});
			api.addEventListener(FBLocalEvent.CONN_ERROR, function (event:FBLocalEvent):void {
				parseError();
			});
			
		}
		
		private function initTest():void {
			with (config.me) {
				uid			= config.compile.randomTestID ? String(Math.round(Math.random()*10000)) : "1";
				first_name	= "test";
				last_name	= "test";
				photo		= "http://cs308128.vk.me/v308128055/487e/L8TX8nDetec.jpg";
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
				friends.push ( config.me );
			}
			
			step = 1/1;
		}
		
		private function set step(value:Number):void {
			progress = value;
			dispatchEvent( new ProgressEvent ( ProgressEvent.PROGRESS ) );
			
			if (value==1) complete();
		}
		
		private function parseError():void {
			this.dispatchEvent( new NetworkEvent ( NetworkEvent.ERROR, {type: GameError.GAME_ERROR, id: 0, text: '%player% / %sandbox% / %network%', params: {player: config.compile.player, sandbox: config.compile.sandboxType, network: config.me.network} } ) );
		}
		
		private function complete():void {
			this.dispatchEvent( new NetworkEvent ( NetworkEvent.COMPLETE ) );
		}
	}
}